//
//  TYZAssociatePickerView.m
//  MetalEquipUnion
//
//  Created by tangbin on 2021/10/25.
//

#import "TYZAssociatePickerView.h"
#import "UIWindow+TYZAssociatePickerView.h"
#import "TYZCommonCellModel.h"

@interface TYZAssociatePickerView () <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) EDSCustomLeftRightBtnView *topView;
@property (nonatomic, strong) UIPickerView *pickerView;
//@property (nonatomic, strong) NSArray *pickerList;
@property (nonatomic, strong) NSArray *pickerTwoList;

@property (nonatomic, strong) NSArray *dataList;

@property (nonatomic, strong) TYZCommonCellModel *firstModel;

@property (nonatomic, strong) TYZCommonCellModel *lastModel;

@end

@implementation TYZAssociatePickerView


- (instancetype)initWithFrame:(CGRect)frame dataList:(NSArray *)dataList
{
    self.dataList = dataList;
    TYZCommonCellModel *model = self.dataList.firstObject;
    self.firstModel = model;
    self.lastModel = model.files.firstObject;
    self.pickerTwoList = model.files.copy;
    return [self initWithFrame:frame];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor whiteColor];
    [self initWithTopView];
    
    [self initWithPickerView];
}

- (void)setTitle:(NSString *)title
{
    _topView.titleLabel.text = title;
}

- (void)initWithTopView
{
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kEDSCustomLeftRightBtnViewHeight);
    _topView = [[EDSCustomLeftRightBtnView alloc] initWithFrame:frame leftTitle:@"取消" leftColor:[UIColor whiteColor] rightTitle:@"确认" rightColor:[UIColor whiteColor]];
    [self addSubview:_topView];
    
    @weakify(self);
    _topView.touchButtonBlock = ^(EN_BUTTON_TYPE_TAG tag) {
        @strongify(self);
        if (tag == EN_BUTTON_TYPE_SUBMIT_TAG)
        {// 确认

            if (self.viewBlock)
            {
                self.viewBlock(@{@"value":self.firstModel, @"valueTwo":self.lastModel});
            }
            [self hide];
        }
        else
        {
            [self hide];
        }
    };
}

- (void)initWithPickerView
{
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, _topView.bottom, kScreenWidth, 216)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
//    _pickerView.showsSelectionIndicator = YES;
    [_pickerView setAutoresizesSubviews:YES];
    [_pickerView setUserInteractionEnabled:YES];
    [_pickerView setContentMode:UIViewContentModeScaleToFill];
    [self addSubview:_pickerView];
}




- (void)show
{
    if (!kCSKeyWindow.associatePickerView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.associatePickerMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.associatePickerView = self;
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - kTYZAssociatePickerViewHeight;
        [UIView animateWithDuration:0.23 animations:^{
            maskView.alpha = 0.4;
            //            self.alpha = 1;
            self.frame = frame;
        }];
    }
}

- (void)hide
{
    if (kCSKeyWindow.associatePickerView)
    {
//        [UIApplication sharedApplication].keyWindow
        @weakify(self);
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight;
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            //            self.alpha = 0;
            self.frame = frame;
            kCSKeyWindow.associatePickerMaskView.alpha = 0;
        } completion:^(BOOL finished) {
            [kCSKeyWindow.associatePickerView removeFromSuperview];
            [kCSKeyWindow.associatePickerMaskView removeFromSuperview];
            kCSKeyWindow.associatePickerMaskView = nil;
            kCSKeyWindow.associatePickerView = nil;
        }];
    }
}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return self.dataList.count;
            break;
        case 1:
            return [self.pickerTwoList count];
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            TYZCommonCellModel *model = [self.dataList objectOrNilAtIndex:row];
            return model.title;
        } break;
        case 1:
        {
            TYZCommonCellModel *model = [self.pickerTwoList objectOrNilAtIndex:row];
            return model.title;
        }
            break;
        default:
            return @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
        {
            TYZCommonCellModel *model = [self.dataList objectOrNilAtIndex:row];
            self.firstModel = model;
            self.pickerTwoList = model.files.copy;
            self.lastModel = model.files.firstObject;
            [self.pickerView selectRow:0 inComponent:1 animated:YES];
            [self.pickerView reloadComponent:1];
        }  break;
        case 1:
            self.lastModel = [self.pickerTwoList objectOrNilAtIndex:row];
            break;
        default:
            break;
    }
    if (self.viewBlock)
    {
        if (self.autoSelected)
        {
            self.viewBlock(@{@"value":objectNull(self.firstModel), @"valueTwo":objectNull(self.lastModel)});
        }
    }
}

@end
